import { createRouter, createWebHashHistory } from 'vue-router'
import Article from '@/views/tabs-page/tab-article/Article.vue'
import useLoginStore from '@/stores/login/login'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  scrollBehavior: () => ({ top: 0 }),
  routes: [
    {
      path: '/',
      redirect: '/article'
    },
    {
      path: '/login',
      name: 'Login',
      meta: {
        showTopbar: true
      },
      component: () => import('@/views/login/Login.vue')
    },
    {
      path: '/profile',
      name: 'Profile',
      meta: {
        showTopbar: true,
        showBottombar: true,
        isAuth: true
      },
      component: () => import('@/views/profile/Profile.vue')
    },
    {
      path: '/article',
      name: 'Article',
      meta: {
        showTopbar: true,
        showBottombar: true,
        keepAlive:true
      },
      component: Article
    },
    {
      path: '/article-detail',
      name: 'Article-detail',
      meta: {
        showTopbar: true,
        showBottombar: true
      },
      component: () => import('@/views/tabs-page/tab-article/ArticleDetail.vue')
    },
    {
      path: '/appspace',
      name: 'AppSpace',
      meta: {
        showTopbar: true,
        showBottombar: true,
        isAuth: true
      },
      component: () => import('@/views/tabs-page/tab-appspace/AppSpace.vue')
    },
    {
      path: '/aimodel',
      name: 'AIModel',
      meta: {
        showTopbar: true,
        showBottombar: true
      },
      component: () => import('@/views/tabs-page/tab-aimodel/AIModel.vue')
    },
    {
      path: '/document',
      name: 'Document',
      meta: {
        showTopbar: true,
        showBottombar: true
      },
      component: () => import('@/views/tabs-page/tab-document/Document.vue')
    }

    // {
    //   name: "maintenance",
    //   path: "/maintenance",
    //   meta: {
    //     title: "系统维护"
    //   },
    //   component: () => import("@/views/404/maintenance.vue")
    // },
    // {
    //   name: '404',
    //   path: '/:pathmatch(.*)',
    //   meta: {
    //     title: '404'
    //   },
    //   component: () => import('@/views/404/404.vue')
    // }
  ]
})

router.beforeEach(to => {
  const loginStore = useLoginStore()
  if (to.meta.isAuth && !loginStore.token) {
    ElMessage({ message: '请先登录!', type: 'warning' })
    return '/login'
  } else if (to.path === '/login' && loginStore.token) {
    return '/'
  }
})

export default router
